package com.bsh.controller.article;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.bsh.mybatis.model.ArticleWithBLOBs;
import com.bsh.service.ArticleService;
import com.bsh.utils.PageTools;

/**
 * 邀请返利
 * 
 * @author Administrator
 *
 */
@Controller
public class ArticleController {
	private static Logger log = LoggerFactory.getLogger(ArticleController.class);

	@Autowired
	HttpServletRequest request;
	@Autowired
	HttpServletResponse response;
	@Autowired
	private ArticleService artServ;

	/**
	 * 修改文章
	 */
	@RequestMapping(value = "/article/edit/{artType}")
	public ModelAndView edit(@PathVariable(value = "artType") String artType) {
		ModelAndView mv = new ModelAndView();
		mv.setViewName("/pages/article/articleEdit");
		ArticleWithBLOBs article = artServ.findByTag("app_"+artType);
		mv.addObject("article", article);
		mv.addObject("artType", artType);
		return mv;
	}

	/**
	 * 保存文章
	 */
	@RequestMapping(value = "/article/save/{artType}")
	public ModelAndView editSave(@ModelAttribute ArticleWithBLOBs dto, @PathVariable(value = "artType") String artType) {
		ModelAndView mv = edit(artType);
		mv.addObject("article", dto);
        dto.setTags("app_"+artType);
        String content= request.getParameter("content");
        dto.setContent(content);
		int res = artServ.saveArticle(dto);

		String msg = "";
		if (res > 0)
			msg = PageTools.alert("修改用户", "保存成功！", 1000);
		else
			msg = PageTools.alert("修改用户", "保存失败！", 1000);
		mv.addObject("actionMsg", msg);
		return mv;
	}
}
